package com.waijiaojun.tpo.web.admin;

import com.waijiaojun.tpo.entity.Scenario;
import com.waijiaojun.tpo.entity.SentencePattern;
import com.waijiaojun.tpo.entity.Vocabulary;
import com.waijiaojun.tpo.rest.RestClientException;
import com.waijiaojun.tpo.rest.RestResponse;
import com.waijiaojun.tpo.rest.RestServerException;
import com.waijiaojun.tpo.service.ScenarioService;
import com.waijiaojun.tpo.service.SentencePatternService;
import com.waijiaojun.tpo.utils.Constants;
import com.waijiaojun.tpo.utils.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xvitcoder
 * Date: 12/20/12
 * Time: 5:27 PM
 */
@Controller
@RequestMapping("/admin")
public class AdminSentencePatternController {

    private static Logger logger = LoggerFactory.getLogger(AdminSentencePatternController.class);
    @Autowired
    private ScenarioService scenarioService;

    @Autowired
    private SentencePatternService sentencePatternService;


    @RequestMapping("article/{articleId}/sentencePattern")
    public String categoryList(@PathVariable("articleId") Long articleId, Model model) {
        logger.info(Constants.prefix_in + "articleId=" + articleId);
        Scenario scenario = scenarioService.findById(articleId);
        model.addAttribute("article", scenario);
        return "admin/sentence-pattern-list";
    }

    @RequestMapping("article/{articleId}/sentencePattern/list/data")
    @ResponseBody
    public List<SentencePattern> articleListData(@PathVariable("articleId") Long articleId) {
        logger.info(Constants.prefix_in + "articleId=" + articleId);
        List<SentencePattern> list = sentencePatternService.findByArticleId(articleId);
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(list));
        return list;
    }

    @RequestMapping("article/{articleId}/sentencePattern/add")
    @ResponseBody
    public RestResponse vocabularyAdd(@PathVariable("articleId") Long articleId) {
        RestResponse restResponse = null;
        try {
            logger.info(Constants.prefix_in + "articleId=" + articleId);//打印入参
            SentencePattern sentencePattern = new SentencePattern();
            sentencePattern.setScenarioId(articleId);
            sentencePatternService.add(sentencePattern);
            restResponse = RestResponse.getSuccessResponse();
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    @RequestMapping("sentencePattern/editShortcutSave")
    @ResponseBody
    public RestResponse vocabularyEditShortcutSave(SentencePattern sentencePattern) {
        RestResponse restResponse = null;
        try {
            logger.info(Constants.prefix_in + JsonUtil.toJson(sentencePattern));//打印入参

            sentencePatternService.update(sentencePattern);
            restResponse = RestResponse.getSuccessResponse();
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    @RequestMapping("sentencePattern/delete/{id}")
    @ResponseBody
    public RestResponse articleDelete(@PathVariable Long id) {
        RestResponse restResponse = null;
        try {
            logger.info(Constants.prefix_in + "id=" + id);//打印入参

            sentencePatternService.delete(id);
            restResponse = RestResponse.getSuccessResponse();
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }


}
